/*
 * @(#)AbstractAuditEventReceiver.java
 * Date 2013-11-04
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.atna.receiver;

import com.nesscomputing.syslog4j.server.SyslogServer;
import com.nesscomputing.syslog4j.server.SyslogServerConfigIF;
import com.nesscomputing.syslog4j.server.SyslogServerEventHandlerIF;
import com.nesscomputing.syslog4j.server.SyslogServerIF;

import com.nesscomputing.syslog4j.util.SyslogUtility;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import us.hornerscorners.lamppost.ihecommon.config.AtnaConfig;
import us.hornerscorners.lamppost.ihecommon.config.AtnaProtocolType;


/**
 * Class description
 *
 *
 * @version        v1.0, 2013-11-04
 * @author         Jim Horner
 */
public abstract class AbstractAuditEventReceiver implements IAuditEventReceiver {

    /** Field description */
    private final AtnaConfig atnaConfig;

    /** Field description */
    private final Logger logger = LoggerFactory.getLogger(getClass());

    /** Field description */
    private final String protocol;

    /**
     * Constructs ...
     *
     *
     * @param atnaConfig
     * @param protocol
     */
    public AbstractAuditEventReceiver(AtnaConfig atnaConfig, AtnaProtocolType protocol) {

        super();
        this.atnaConfig = atnaConfig;
        this.protocol = String.format("atna-%s", protocol.name());
    }

    /**
     * Method description
     *
     *
     * @param handler
     */
    @Override
    public void addEventHandler(SyslogServerEventHandlerIF handler) {

        SyslogServerIF syslog = SyslogServer.getInstance(getProtocol());
        SyslogServerConfigIF sysconfig = syslog.getConfig();

        sysconfig.addEventHandler(handler);
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public AtnaConfig getAtnaConfig() {

        return this.atnaConfig;
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public String getProtocol() {
        return protocol;
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public SyslogServerConfigIF getSyslogConfig() {

        return getSyslogServer().getConfig();
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public SyslogServerIF getSyslogServer() {

        return SyslogServer.getInstance(this.protocol);
    }

    /**
     * Method description
     *
     *
     * @return
     */
    @Override
    public boolean isStarted() {

        return getSyslogServer().isStarted();
    }

    /**
     * Method description
     *
     */
    @Override
    public void shutdown() {

        logger.info("Shutting down Server...");
        SyslogServer.getInstance(getProtocol()).shutdown();
    }

    /**
     * Method description
     *
     */
    @Override
    public void startup() {

        SyslogServerConfigIF sysconfig = getSyslogConfig();

        logger.info("Staring Server [{}] on {}: {}.", this.protocol, sysconfig.getHost(),
                    sysconfig.getPort());

        // Start server
        SyslogServer.getThreadedInstance(this.protocol);

        while (true) {

            SyslogUtility.sleep(1000);
        }
    }
}
